package com.example;

import android.os.AsyncTask;
import android.util.Log;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: fransvanderlek
 * Date: 6/17/12
 * Time: 11:23 AM
 * To change this template use File | Settings | File Templates.
 */
public abstract class SynchronizationTask extends AsyncTask<List<File>, List<File>, List<File>> {
    protected MainViewHolder view;
    protected FTPClient ftpClient;

    public SynchronizationTask(MainViewHolder view, FTPClient ftpClient) {
        this.view = view;
        this.ftpClient = ftpClient;


    }

    @Override
    protected void onProgressUpdate(List<File>... files) {

        Log.d("UPLOAD", "Update view contents");

        List<File> updatedList = new ArrayList<File>();
        updatedList.addAll(files[0]);

        updateView(updatedList);


    }

    private void updateView(List<File> updatedList) {

        ImageAdapter adapter = (ImageAdapter) view.dataView.getAdapter();

        if ( adapter == null ){
            view.dataView.setAdapter(new ImageAdapter(updatedList, view.dataView.getContext()));
        }else{
            view.dataView.setAdapter(new ImageAdapter(updatedList,  view.dataView.getContext()));
        }


        MessageBarTextBuilder textBuilder = new MessageBarTextBuilder(updatedList);

        view.messagesView.setText(textBuilder.buildText());
    }

    @Override
    protected void onPostExecute(List<File> files)
    {
        try {
            ftpClient.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
